package com.zhf.educourseboot.service.impl;

import com.zhf.eduapi.entity.Course;
import com.zhf.educourseboot.mapper.CourseMapper;
import com.zhf.educourseboot.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2021/2/16 11:40
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public List<Course> getAllCourse() {
//        //1.去Redis查询数据集合
//        System.out.println("查询Redis-----------------------------------");
//        List<Course> list = (List<Course>)redisTemplate.opsForValue().get("allCourses");
//        //2.如果Redis中没有的话，去MySQL查询
//        if (list ==null){
//            synchronized(this){
//                list = (List<Course>)redisTemplate.opsForValue().get("allCourses");
//                if (list ==null) {
//                    list = courseMapper.getAllCourse();
//                }
//            }
//
//        }
//        //3.将数据集合放入Redis以便下次查询
//        redisTemplate.opsForValue().set("allCourses",list,10);
        List<Course> list=list = courseMapper.getAllCourse();
        return list;
    }

    @Override
    public List<Course> getCourseByUserId(String userId) {
        return courseMapper.getCourseByUserId(userId);
    }

    @Override
    public Course getCourseById(int id) {
        return courseMapper.getCourseById(id);
    }

    @Override
    public List<Course> getMyCoursesById(List<String> idList) {
        return courseMapper.getMyCoursesById(idList);
    }
}
